package com.shengyuan.manage_os.old.ying.erpm;


import org.json.JSONException;
import org.json.JSONObject;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.controls.JieXieMingCheng;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_JiShi_Table;
import com.shengyuan.manage_os.old.ying.erpm.db.CLS_Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.os.Bundle;
import android.widget.EditText;

public class Actual_Form_TaiBanJieSu_JX extends Actual_Form_TaiBanKaiShi_JX {

	//protected PictureButton PictureButton_XiaoShiBiao, PictureButton_YouBiao;
	protected EditText editText_startxiaoshi;
	
	double iValue, iHour;
	@Override
	public String MyID() {
		return getClass().getName();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState,true);
		if(WorkSpace_TaiBanActivity_JX.ButtonIndex==0)//自有机械
		{
			setContentView(R.layout.actual_form_taiban_ziyoujixie_jixie_jiesua);
		}else {
			setContentView(R.layout.actual_form_taiban_ziyoujixie_jiesua);
		}
		
		InitControls();
		editText_startxiaoshi = (EditText)findViewById(R.id.editText_startxiaoshi);
		int id = WorkSpace_TaiBanActivity_JX.mMachinery.Id;
		
		Actual_Data_Table dbo = new Actual_Data_Table(this);
		CLS_Actual_Data_Table cls= dbo.get_Json1Data(id);
		try {
			JSONObject jObject = new JSONObject(cls.JSON1);
			iHour = Double.parseDouble( jObject.getString("小时表数:"));
			editText_startxiaoshi.setText(iHour +"");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void SaveJsonData() {
		IsJson1 = false; mFinished = 1; mMaxid= WorkSpace_TaiBanActivity_JX.mMachinery.Id ;
		SaveData();
	}
	
	protected void SaveData(){
		try {
			IsPassed();
			JsonMyID();
			
			getJieXieMingCheng();
			getZhongLiang();
			getHowLongTime();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
			JsonPicture_Ext();
			//价格保存
			if (Submit_System_Setting_Data_Task.mID>=0) {
				iValue = Double.parseDouble(editText_helishijian.getText().toString());
				String YuSuanJiaGe = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("YuSuanJiaGe");
				Double val =  Double.parseDouble(YuSuanJiaGe) * iValue;
				String s = val + "";
				mJson.put( getString(R.string.title_actual_form_yusuanjiage), s);
				mJson.put( getString(R.string.title_actual_form_jiage),  YuSuanJiaGe);
			}
			mJson.put( getString(R.string.title_actual_form_jiage),  iHour+"");
		}catch (Exception e) {
		}
	}
	@Override
	protected void Save_Has_Finished() {
        Actual_JiShi_Table dbo= new Actual_JiShi_Table(this);
        dbo.Save_FinishedRecord(JieXieMingCheng.mMachinery.Name);
        dbo.close();
	}
	
	private void getHowLongTime() throws JSONException {
		mJson.put( getString(R.string.title_actual_form_edittext_jixieshijian), WorkSpace_TaiBanActivity_JX.mMachinery.ElapsedTime);
		mJson.put( getString(R.string.title_actual_form_edittext_jixieshiduan), WorkSpace_TaiBanActivity_JX.mMachinery.WorksTime);
	}
	
	@Override
	public Boolean IsPassed() {
		
		if(!CheckEditNumber(editText_xiaoshibiaoshu))
			return false;
		if(!CheckEditNumber(editText_helishijian))
			return false;		
		
		Actual_Data_Table dbo = new Actual_Data_Table(this);
		CLS_Actual_Data_Table cls= dbo.get_Json1Data(WorkSpace_TaiBanActivity_JX.mMachinery.Id);
		try {
			JSONObject jObject = new JSONObject(cls.JSON1);
			double iHour = Double.parseDouble( jObject.getString("小时表数:"));
			double lastHour = Double.parseDouble( editText_xiaoshibiaoshu.getText().toString());
			iValue = lastHour - iHour; 
			if (lastHour<iHour) {
				Utility.MessageBox(this,"错误的小时表数，不能小于开始小时数："+iHour);
				return false;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	
		if (GpsButton_Gps.x ==0 && GpsButton_Gps.y==0) {
			Utility.MessageBox( this,GpsButton_Gps.hint );
			return false;
		}
		if (!PictureButton_Chexing.IsExist()) {
			Utility.MessageBox(this, PictureButton_Chexing.FIELD );
			return false;
		}		
		if (!PictureButton_XiaoShiBiao.IsExist()) {
			Utility.MessageBox(this, PictureButton_XiaoShiBiao.FIELD );
			return false;
		}
//		if (!PictureButton_YouBiao.IsExist()) {
//			Utility.MessageBox( PictureButton_YouBiao.FIELD );
//			return false;
//		}		

		return true;
	}	
}
